package com.word.web.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.word.web.model.Response;
import com.word.web.service.JsonFileReaderService;
import com.word.web.service.LocalHotpotAPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("web")
public class WebController {
    @Autowired
    private JsonFileReaderService jsonFileReaderService;
    @Autowired
    private LocalHotpotAPIService localHotpotAPIService;

    /**
     * 获取活动分页数据
     */
    @GetMapping
    public Response get() {
        try {
            String data;
            if (Math.random() > 0.7f) {
                JsonNode jsonNode = jsonFileReaderService.getNode();
                data = jsonNode.get("word").asText();
            } else {
                data = localHotpotAPIService.get();
                if (data == null) {
                    JsonNode jsonNode = jsonFileReaderService.getNode();
                    data = jsonNode.get("word").asText();
                }
            }
            return Response.dataSuccess(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail(e.getMessage());
        }
    }
}
